package com.wu.util;

import org.apache.commons.collections.CollectionUtils;
import org.dozer.DozerBeanMapper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 类转换
 */
public class DozerUtil {
	
	private static DozerBeanMapper mapp = null;
    
	public static DozerBeanMapper getMapper() {
		if (mapp == null) {
			mapp = new DozerBeanMapper();
		}
		return mapp;
	}
    
    public static <T> T map(Object source, Class<T> destinationClass) {
	    if (source == null) {
	        return null;
        }
        return getMapper().map(source, destinationClass);
    }
    
	public static <T> List<T> mapList(Collection<?> sourceList, Class<T> destinationClass) {
        List<T> destinationList = new ArrayList<T>();
        if (CollectionUtils.isEmpty(sourceList)) {
            return destinationList;
        }
        for (Object sourceObject : sourceList) {
            T destinationObject = getMapper().map(sourceObject, destinationClass);
            destinationList.add(destinationObject);
        }
        return destinationList;
    }
}
